package edu.webtech.project.server.domain;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import edu.webtech.project.server.PMF;
import edu.webtech.project.shared.FileDataDTO;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class FileData {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	
	@Persistent
	private String fileName;
	
	@Persistent
	private String semesterName;
	
	@Persistent
	private String courseName;
	
	@Persistent
	private String subjectName;
	
	@Persistent
	private String url;
	
	public FileData() {}
	
	public FileData(String fileName, String semesterName, String courseName, String url) {
		this.fileName     = fileName;
		this.semesterName = semesterName;
		this.courseName   = courseName;
		this.url          = url;
	}

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getFileName() {
		return this.fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getSemesterName() {
		return this.semesterName;
	}

	public void setSemesterName(String semesterName) {
		this.semesterName = semesterName;
	}

	public String getCourseName() {
		return this.courseName;
	}

	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	
	public String getSubjectName() {
		return this.subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}
	
	public String getUrl() {
		return this.url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	@SuppressWarnings("unchecked")
	public static List<FileData> getAllFiles() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<FileData> multipleResult = null, detached = null;
		
		Query q = pm.newQuery(FileData.class);
		try {
			
			multipleResult = (List<FileData>) q.execute();
			if (multipleResult != null) {
				detached = new ArrayList<FileData>();			
				for (FileData obj: multipleResult) {
					detached.add(pm.detachCopy(obj));
				}	
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		
		return detached;
	}
	
		
	@SuppressWarnings("unchecked")
	public static List<FileData> searchFiles(String fileName, String semesterName, String courseName) {

				
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<FileData> multipleResult = null, detached = null;
		
		String strQ = new String();
		
		if (semesterName.compareToIgnoreCase("select semester") != 0) {
			strQ += "semesterName == '";
			strQ += semesterName;
			strQ += "' ";
		}
		
		if (courseName.compareToIgnoreCase("select course") != 0 && 
				semesterName.compareToIgnoreCase("select semester") != 0)  {
			strQ += "&& courseName == '";
			strQ += courseName;
			strQ += "' ";
		} else if (courseName.compareToIgnoreCase("select course") != 0) {
			strQ += "courseName == '";
			strQ += courseName;
			strQ += "' ";
		}
		
		
		Query q = pm.newQuery(FileData.class, strQ);		
		try {
			
			multipleResult = (List<FileData>) q.execute(fileName.toUpperCase());
			if (multipleResult != null) {
				detached = new ArrayList<FileData>();
				
				if (fileName.isEmpty() == false) {
					for (FileData object: multipleResult) {
						if (object.getFileName().toLowerCase().contains(fileName.toLowerCase()) == true) {
							detached.add(pm.detachCopy(object));
						}
					}
				} else {
					for (FileData object: multipleResult) {
						detached.add(pm.detachCopy(object));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pm.close();
		}
		
		return detached;
	}
	
	public static List<FileDataDTO> toDTO(List<FileData> files) {
		
		if (files == null) {
			return null;
		}
		
		List<FileDataDTO> dtoList = new ArrayList<FileDataDTO>();
		
		for (int i = 0; i < files.size(); i++) {
			FileDataDTO dto = new FileDataDTO();
			dto.setFileName(files.get(i).getFileName());
			dto.setSemesterName(files.get(i).getSemesterName());
			dto.setCourseName(files.get(i).getCourseName());
			dto.setSubjectName(files.get(i).getSubjectName());
			dto.setUrl(files.get(i).getUrl());
			
			dtoList.add(dto);
		}
		
		return dtoList;
	}
	

}
